package com.xmy.cultivate.controller;

import com.xmy.cultivate.entity.EnrollTeam;
import com.xmy.cultivate.entity.EnrollTeamMember;
import com.xmy.cultivate.service.IEnrollTeamMemberService;
import com.xmy.cultivate.service.IEnrollTeamService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 招生战队成员 前端控制器
 * </p>
 *
 * @author min
 * @since 2023-02-10
 */
@RestController
@RequestMapping("/api/enrollTeamMember")
@Scope("prototype")
public class EnrollTeamMemberController extends BaseController{

    @Autowired
    IEnrollTeamMemberService iEnrollTeamMemberService;

    /**
     * 切换战队
     * @param enrollTeamMember
     * @return
     */
    @PutMapping("/switchTeam")
    public Result switchTeam(@RequestBody EnrollTeamMember enrollTeamMember){

        if(enrollTeamMember.getTeamId()==null){
            return new Result(Code.ERROR,"","转入战队id不能为空");
        }
        if(enrollTeamMember.getEndDate()==null){
            return new Result(Code.ERROR,"","日期不能为空");
        }

        //由于id用了teacherId传过来，因为这里使用teahcerId
        EnrollTeamMember enrollTeamMemberInfo = iEnrollTeamMemberService.getById(enrollTeamMember.getTeacherId());
        if(enrollTeamMemberInfo==null){
            return new Result(Code.ERROR,"","数据不存在");
        }

        if (enrollTeamMember.getEndDate().isBefore(enrollTeamMemberInfo.getStartDate())){
            return new Result(Code.ERROR,"","结束时间不能小于现在所在战队开始时间");
        }
        enrollTeamMember.setTeacherId(enrollTeamMemberInfo.getTeacherId());



        boolean rs = iEnrollTeamMemberService.switchTeam(enrollTeamMember);
        return new Result(rs?Code.OK:Code.ERROR);
    }
}
